﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.Templates;

namespace RibbonEditor.Controls.Templates
{
    public partial class SectionControl : UserControl, IControlRulable
    {
        private Section innerSection;

        public Section InnerSection
        {
            get { return innerSection; }
            set { innerSection = value; }
        }

        public SectionControl()
        {
            InitializeComponent();

            this.ecAlignment.Values = Enum.GetValues(typeof(Enumerations.SectionAlignment));
            this.ecSectionType.Values = Enum.GetValues(typeof(Enumerations.SectionType));

            this.innerSection = new Section();
            this.FillControls();
        }

        public SectionControl(Section section)
            : this()
        {
            this.innerSection = section;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                this.ecAlignment.SelectedValue = this.innerSection.Alignment.Value.ToString();
                this.ecSectionType.SelectedValue = this.innerSection.Type.Value.ToString();
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerSection.Alignment = (Enumerations.SectionAlignment)Enum.Parse(typeof(Enumerations.SectionAlignment), this.ecAlignment.SelectedValue);
                    this.innerSection.Type = (Enumerations.SectionType)Enum.Parse(typeof(Enumerations.SectionType), this.ecSectionType.SelectedValue);

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void OverflowSectionControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IControlRulable Members


        public void BuildObjectXml(System.Xml.XmlNode parentNode)
        {
            try
            {
                this.innerSection.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
